package com.example.question.array;

import java.util.ArrayList;
import java.util.List;

public class Code03_1002 {

    public static void main(String[] args) {
        Code03_1002 code = new Code03_1002();
        List<String> list = code.commonChars(new String[]{"bella", "label", "roller"});
        System.out.println(list);
    }


    public List<String> commonChars(String[] words) {
        // 二维数组
        int m = words.length;
        // 记录每个字符出现的次数
        int[][] countMap = new int[m][26];
        for (int i = 0; i < m; i++) {
            char[] chars = words[i].toCharArray();
            for (char aChar : chars) {
                countMap[i][aChar - 'a']++;
            }
        }
        List<String> result = new ArrayList<>();
        for (int i = 0; i < 26; i++) {
            int min = Integer.MAX_VALUE;
            for (int j = 0; j < m; j++) {
                min = Math.min(min, countMap[j][i]);
            }
            for (int j = 0; j < min; j++) {
                char c = (char) (i + 'a');
                result.add(String.valueOf(c));
            }
        }
        return result;
    }
}
